package io.itit.ecp.admin.provide.action.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import icu.mhb.mybatisplus.plugln.core.JoinLambdaWrapper;
import icu.mhb.mybatisplus.plugln.extend.Joins;
import io.itit.ecp.admin.provide.action.req.AIModelQueryReq;
import io.itit.ecp.admin.provide.action.resp.AIModelResp;
import io.itit.ecp.admin.server.entity.AIModelEntity;
import io.itit.ecp.admin.server.entity.SceneProjectEntity;
import io.itit.ecp.admin.server.service.IAIModelService;
import io.itit.grass.common.provide.action.req.BaseIdReq;
import io.itit.grass.common.provide.action.resp.ResultResp;
import io.itit.grass.common.provide.action.validation.IAdd;
import io.itit.grass.common.provide.action.validation.IQueryPage;
import io.itit.grass.common.provide.action.validation.IRequired;
import io.itit.grass.common.provide.action.validation.IUpdate;
import io.itit.grass.web.wall.utils.PageUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;


/**
 * AI Model控制类
 */
@Api(tags = "AI模型管理")
@RestController
@RequestMapping("/ecp-admin/aimodel")
public class AIModelController {

    @Resource
    private IAIModelService aiModelService;

    @ApiOperation("分页查询")
    @PostMapping("queryPage")
    @RequiresPermissions("ecp-admin:aimodel:search")
    public ResultResp<IPage<AIModelResp>> queryPage(@RequestBody @Validated(IQueryPage.class) AIModelQueryReq req) {
        Page<AIModelResp> page = buildQW(req).joinPage(PageUtils.getPage(req, AIModelResp.class), AIModelResp.class);
        return ResultResp.success(page);
    }

    @ApiOperation("新增")
    @PostMapping("add")
    @RequiresPermissions("ecp-admin:aimodel:add")
    public ResultResp<Boolean> add(@RequestBody @Validated({IAdd.class}) AIModelEntity req) {
        return ResultResp.success(aiModelService.save(req));
    }

    @ApiOperation("编辑")
    @PostMapping("update")
    @RequiresPermissions("ecp-admin:aimodel:update")
    public ResultResp<Boolean> update(@RequestBody @Validated({IUpdate.class}) AIModelEntity req) {
        return ResultResp.success(aiModelService.updateById(req));
    }

    @ApiOperation("删除(根据Id删)")
    @PostMapping("deleteById")
    @RequiresPermissions("ecp-admin:aimodel:deleteById")
    public ResultResp<Boolean> deleteById(@RequestBody @Validated({IRequired.class}) BaseIdReq req) {
        return ResultResp.success(aiModelService.removeById(req.getId()));
    }

    private static JoinLambdaWrapper<AIModelEntity> buildQW(AIModelQueryReq req) {
        return Joins.of(AIModelEntity.class)
                .leftJoin(SceneProjectEntity.class, SceneProjectEntity::getId, AIModelEntity::getSceneProjectId)
                .selectAs(SceneProjectEntity::getProjectName, AIModelResp::getProjectName)
                .end()
                .eq(StringUtils.hasLength(req.getSceneProjectId()), AIModelEntity::getSceneProjectId, req.getSceneProjectId())
                .like(StringUtils.hasLength(req.getModelName()), AIModelEntity::getModelName, req.getModelName())
                .orderByDesc(AIModelEntity::getCreateTm);
    }
}


